package com.lanou.dllo.soccernews.fragment.importantfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.adapter.ImportantAdapter;
import com.lanou.dllo.soccernews.bean.ImportBean;
import com.lanou.dllo.soccernews.view.SpaceItemDecoration;
import com.lanou.dllo.soccernews.volley.VolleySingleton;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/29.
 */
public class ImportantFragment extends Fragment {
    private RecyclerView im_recycler;
    private ImportantAdapter importantAdapter;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_important, null);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        im_recycler = (RecyclerView) view.findViewById(R.id.im_rcview);
        LinearLayoutManager lm = new LinearLayoutManager(getContext());
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        im_recycler.setLayoutManager(lm);

        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.space);
        im_recycler.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
        importantAdapter = new ImportantAdapter();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addDatas();
        im_recycler.setAdapter(importantAdapter);
    }

    public void addDatas() {
        SimpleDateFormat formatter    =   new    SimpleDateFormat    ("yyyy-MM-dd ");
        Date curDate    =   new    Date(System.currentTimeMillis());//获取当前时间
        String    str    =    formatter.format(curDate);

        Log.i("lanoulllll",str);

        String url = "https://data.dongqiudi.com/soccerDataAPI/interface/getNewMatchlistByDate.php?start=2015-10-30";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i("lanou","ffffs" +""+response);
                        Gson gson = new Gson();
                        ImportBean importBean = gson.fromJson(response, ImportBean.class);
                        Log.i("lanou",importBean+"哦哦哦哦哦");
                        if (importBean != null) {
                            List<ImportBean.ListEntity> list = importBean.getList();

                            if (list != null && list.size() > 0) {
                                importantAdapter.addData(list);
                            }

                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);
    }
}
